
set(GDAL_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/frmts)
set(GDAL_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/frmts)
include_directories(${GDAL_CURRENT_SOURCE_DIR})
include_directories(${GDAL_CURRENT_BINARY_DIR})

# needed packages
find_package(JPEG REQUIRED)
if(JPEG_FOUND)
    include_directories(${JPEG_INCLUDE_DIR})
    add_definitions(-DHAVE_JPEG)
endif(JPEG_FOUND) 
    
add_definitions(-DUSE_CPL)
add_definitions(-DPAM_ENABLED)

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/ogr" "${CMAKE_CURRENT_SOURCE_DIR}/ogr/ogrsf_frmts")

option(GDAL_BMP "Build bmp driver" YES)
option(GDAL_GIF "Build gif driver" YES)
option(GDAL_TIF "Build tif driver" YES)
option(GDAL_IMG "Build ERDAS img driver" YES)
option(GDAL_JPEG "Build jpeg driver" YES)
option(GDAL_MEM "Build mem driver" YES)
option(GDAL_PNG "Build png driver" YES)
option(GDAL_RAW "Build raw driver" YES)
option(GDAL_SAGA "Build saga driver" YES)
option(GDAL_TIL "Build til driver" YES)
option(GDAL_VRT "Build vrt driver" YES)

#add_files_macro("../gcore" "gcore")
set(GDAL_HHEADERS ${GDAL_HHEADERS}
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdaljp2metadata.h
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_frmts.h
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_pam.h
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_priv.h
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_proxy.h
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_rat.h
)

set(GDAL_CSOURCES ${GDAL_CSOURCES}
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalmajorobject.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdaldataset.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdaldriver.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/rasterio.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalrasterblock.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalnodatamaskband.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalcolortable.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalrasterband.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalallvalidmaskband.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalnodatavaluesmaskband.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdaldrivermanager.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdaldefaultasync.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalmultidomainmetadata.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdaldefaultoverviews.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalopeninfo.cpp    
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalpamdataset.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalpamrasterband.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalpamproxydb.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_rat.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalproxypool.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalproxydataset.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/overview.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdaldllmain.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdalgmlcoverage.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdaljp2box.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdaljp2metadata.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_rpcimdio.cpp
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/gcore)

#add_files_macro("../alg" "alg")
set(GDAL_ALGHHEADERS
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalgrid.h
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalwarper.h
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalwarpkernel_opencl.h
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdal_alg.h
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdal_alg_priv.h
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gvgcpfit.h
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/thinplatespline.h 
)
set(GDAL_HHEADERS ${GDAL_HHEADERS} ${GDAL_ALGHHEADERS})
source_group("Header Files\\alg" FILES ${GDAL_ALGHHEADERS})
  
set(GDAL_ALGCSOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/contour.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/fpolygonize.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalchecksum.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalcutline.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdaldither.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalgeoloc.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalgrid.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalmediancut.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalproximity.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalrasterfpolygonenumerator.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalrasterize.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalrasterpolygonenumerator.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalsievefilter.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalsimplewarp.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdaltransformer.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalwarper.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalwarpkernel.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalwarpkernel_opencl.c
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdalwarpoperation.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdal_crs.c
    #gdal_nrgcrs.c
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdal_rpc.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/gdal_tps.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/llrasterize.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/polygonize.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/rasterfill.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/alg/thinplatespline.cpp 
)
set(GDAL_CSOURCES ${GDAL_CSOURCES} ${GDAL_ALGCSOURCES})
source_group("Source Files\\alg" FILES ${GDAL_ALGCSOURCES})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/alg)

add_files_macro("${GDAL_CURRENT_SOURCE_DIR}" "frmts")

if(ZLIB_FOUND)
    add_definitions(-DZIP_SUPPORT)
endif(ZLIB_FOUND)

if(GDAL_BMP)
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/bmp" "frmts\\\\bmp")
    add_definitions(-DFRMT_bmp)
endif()

if(GDAL_GIF)
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/gif" "frmts\\\\gif")
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/gif/giflib" "frmts\\\\gif\\\\giflib")
    include_directories(${GDAL_CURRENT_SOURCE_DIR}/gif/giflib)
    add_definitions(-DFRMT_gif)
endif()

include_directories(${GDAL_CURRENT_SOURCE_DIR}/gtiff)
if(GDAL_TIF)#TODO: check internal / external tif
    find_package(TIFF REQUIRED)
    if(TIFF_FOUND)
        include_directories(${TIFF_INCLUDE_DIR})
        add_definitions(-DHAVE_TIFF)
    endif(TIFF_FOUND)

    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/gtiff" "frmts\\\\tif")
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/gtiff/libgeotiff" "frmts\\\\tif\\\\libgeotiff")
    include_directories(${GDAL_CURRENT_SOURCE_DIR}/gtiff/libgeotiff)
    add_definitions(-DFRMT_gtiff -DBIGTIFF_SUPPORT -DPIXARLOG_SUPPORT)
    if(JPEG_FOUND)
        add_definitions(-DJPEG_SUPPORT -DOJPEG_SUPPORT)
    endif(JPEG_FOUND)
endif()

if(GDAL_IMG)
    #add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/hfa" "frmts\\\\img")
set(GDAL_IMGHHEADERS
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfa.h
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfa_p.h
)
set(GDAL_HHEADERS ${GDAL_HHEADERS} ${GDAL_IMGHHEADERS})
source_group("Header Files\\\\frmts\\\\img" FILES ${GDAL_IMGHHEADERS})
  
set(GDAL_IMGCSOURCES
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfaband.cpp
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfacompress.cpp
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfadataset.cpp
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfadictionary.cpp
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfaentry.cpp
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfafield.cpp
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfaopen.cpp
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfatype.cpp
    ${GDAL_CURRENT_SOURCE_DIR}/hfa/hfa_overviews.cpp
    #hfatest.cpp
)
set(GDAL_CSOURCES ${GDAL_CSOURCES} ${GDAL_IMGCSOURCES})
source_group("Source Files\\\\frmts\\\\img" FILES ${GDAL_IMGCSOURCES})       
    add_definitions(-DFRMT_hfa)
endif()

if(GDAL_JPEG)#TODO: check internal / external jpeg
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/jpeg" "frmts\\\\jpeg")
    add_definitions(-DFRMT_jpeg)
    if(JPEG_FOUND)
        add_definitions(-DJPEG_SUPPORT -DOJPEG_SUPPORT)
    endif(JPEG_FOUND)
endif()

if(GDAL_MEM)
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/mem" "frmts\\\\mem")
    add_definitions(-DFRMT_mem)
endif()

if(GDAL_RAW)
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/raw" "frmts\\\\raw")
    add_definitions(-DFRMT_raw)
endif()

if(GDAL_TIL)
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/til" "frmts\\\\til")
    include_directories(${GDAL_CURRENT_SOURCE_DIR}/vrt)
    add_definitions(-DFRMT_til)
endif()

if(GDAL_SAGA)
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/saga" "frmts\\\\saga")
    add_definitions(-DFRMT_saga)
endif()

if(GDAL_VRT)
    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/vrt" "frmts\\\\vrt")
    include_directories(${GDAL_CURRENT_SOURCE_DIR}/raw)
    add_definitions(-DFRMT_vrt)
endif()

if(GDAL_PNG)#TODO: check internal / external png
    find_package(PNG REQUIRED)
    if(PNG_FOUND)
        include_directories(${PNG_INCLUDE_DIR})
        add_definitions(-DHAVE_PNG ${PNG_DEFINITIONS})
    endif(PNG_FOUND)

    add_files_macro("${GDAL_CURRENT_SOURCE_DIR}/png" "frmts\\\\png")
    add_definitions(-DFRMT_png)
endif()




  